<?php
/**
 * Created by PhpStorm.
 * User: wyq
 * Date: 2021/7/19
 * Time: 16:32
 */

class UserController extends AppController
{
    public $components = array('Session','Email','publicFunction');
    public $helpers =array('Html', 'Form');
    public $layout = null;

    /*
     * 用户列表
     * 获取文章全部信息
     * 渲染页面
     */
    public function index(){
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $limit = 10;
        $count = $this->User->count();
        $pageCount = ceil($count/$limit);
        $params = $this->User->findUser($page,$limit);
        //获取省市信息
        $res = $this->User->getCity(0);
        $this->set(array('params'=>$params,'page'=>$page,'pageCount'=>$pageCount,'address'=>$res));
    }
    /*
     * 新增、修改用户
     *
     */
    public function dell(){
        $method = $_SERVER["REQUEST_METHOD"] ? $_POST : $_GET;
        $data = $this->publicFunction->filterParams($method);
        if (!empty($data['address'])) {
            $resJson = $this->publicFunction->getLocationByAddress($data['address']);
            $resArray = json_decode($resJson, true);
            if ($resArray['status']) {
                $data['location'] = $resArray['geocodes'][0]['location'];
                $data['city'] = $resArray['geocodes'][0]['city'];
                $data['province'] = $resArray['geocodes'][0]['province'];
                $data['district'] = $resArray['geocodes'][0]['district'];
            }
        }
        unset($data['address']);
        $res = $this->User->dellAddress($data);
        var_dump($res);die;
        if ($res){
            $this->publicFunction->success($res);
        }else{
            $this->publicFunction->fail();
        }
    }

    public function rucksack()
    {
        $params = $_POST;
        $res = [];
        if ($params['user_id']){
           $res = $this->User->rucksack($params['user_id']);
        }
        $this->set(['params'=>$res,'uid'=>$params['user_id']]);
    }


    public function changeAddress()
    {
        $method = $_SERVER['REQUEST_METHOD'] ? $_POST : $_GET;
        $data = $this->publicFunction->filterParams($method);
        $res = $this->User->getCity($data['id']);
        $res1 = [];
        foreach ($res as $k => $v){
            $res1[] = $v['citys'];
        }
        $this->publicFunction->success($res1);
    }
}